package com.j.lemon.learn.thread.threadLocal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo {
//    public static final SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>();
    public static class parseDate implements Runnable{
        int i=0;

        parseDate(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            try {
                if(threadLocal.get()==null){
                    System.out.println(Thread.currentThread().getName());
                    threadLocal.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                }
                Date date = threadLocal.get().parse("2018-04-27 19:29:"+i%60);
                System.out.println(i+":"+date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for(int j=0;j<1000;j++){
            executorService.execute(new parseDate(j));
        }
    }


}
